package com.fa4j.system.login.config;

import com.fa4j.system.account.api.AccountLogClient;
import com.fa4j.system.app.AppClient;
import com.fa4j.system.login.LoginSessionClient;
import com.fa4j.system.login.interceptor.SystemLoginHandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@AutoConfiguration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class SystemLoginAutoConfiguration implements WebMvcConfigurer {

    @Bean
    public SystemLoginHandlerInterceptor loginHandlerInterceptor(
            @Lazy AppClient appClient,
            @Lazy LoginSessionClient loginSessionClient,
            @Lazy AccountLogClient accountLogClient

    ) {
        return new SystemLoginHandlerInterceptor(appClient, loginSessionClient, accountLogClient);
    }


    @Bean
    @Order(-1)
    public WebMvcConfigurer authAutoConfigurer(
            SystemLoginHandlerInterceptor systemLoginHandlerInterceptor) {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(systemLoginHandlerInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns("/swagger-ui.html", "/webjars/**", "/static/**", "/error", "/v3/**");
            }
        };
    }


}
